Lambda module
Lambda
Bases: Module
Wraps an arbitrary callable as a Module.
The Lambda module allows you to plug a plain function (sync or async)
into a Synalinks program without having to subclass Module yourself. It is
useful for inserting custom, stateless data transformations into the
program graph.
Because the output schema cannot in general be inferred from an arbitrary
callable, you must provide either a data_model (a DataModel subclass)
or a schema (a JSON schema dict) describing the shape of the function's
return value.
The wrapped function is invoked with a JsonDataModel as its single
positional argument and is expected to return either:
- a
dictmatching the output schema, - a
DataModelorJsonDataModelinstance, or None.
Any callable works — Python lambdas, named sync functions, or named
async functions are all supported. Named, decorated functions are required
when you need the program to be serializable; lambdas cannot be saved.
Inline lambda for short transforms:
import synalinks
class Query(synalinks.DataModel):
query: str
class UppercaseQuery(synalinks.DataModel):
query: str
x0 = synalinks.Input(data_model=Query)
x1 = await synalinks.Lambda(
function=lambda x: {"query": x.get_json()["query"].upper()},
data_model=UppercaseQuery,
)(x0)
Named sync function returning a dict:
@synalinks.saving.register_synalinks_serializable()
def uppercase(inputs):
data = inputs.get_json()
return {"query": data["query"].upper()}
x1 = await synalinks.Lambda(
function=uppercase,
data_model=UppercaseQuery,
)(x0)
Named async function (useful when the transform performs I/O):
@synalinks.saving.register_synalinks_serializable()
async def uppercase(inputs):
data = inputs.get_json()
return {"query": data["query"].upper()}
x1 = await synalinks.Lambda(
function=uppercase,
data_model=UppercaseQuery,
)(x0)
Returning a DataModel instance instead of a dict:
@synalinks.saving.register_synalinks_serializable()
async def uppercase(inputs):
return UppercaseQuery(query=inputs.get_json()["query"].upper())
x1 = await synalinks.Lambda(
function=uppercase,
data_model=UppercaseQuery,
)(x0)
Using a raw JSON schema instead of a DataModel:
x1 = await synalinks.Lambda(
function=lambda x: {"query": x.get_json()["query"].upper()},
schema=UppercaseQuery.get_schema(),
)(x0)
Returning None to short-circuit downstream branches:
# Filter: forward the input only when the query is non-empty,
# otherwise emit None and let downstream `|` / `Branch` skip it.
@synalinks.saving.register_synalinks_serializable()
async def non_empty(inputs):
data = inputs.get_json()
return data if data.get("query") else None
x1 = await synalinks.Lambda(
function=non_empty,
data_model=Query,
)(x0)
Full program example:
import synalinks
import asyncio
async def main():
class Query(synalinks.DataModel):
query: str
class UppercaseQuery(synalinks.DataModel):
query: str
@synalinks.saving.register_synalinks_serializable()
async def uppercase(inputs):
data = inputs.get_json()
return {"query": data["query"].upper()}
x0 = synalinks.Input(data_model=Query)
x1 = await synalinks.Lambda(
function=uppercase,
data_model=UppercaseQuery,
)(x0)
program = synalinks.Program(
inputs=x0,
outputs=x1,
name="shouter",
)
if __name__ == "__main__":
asyncio.run(main())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable
|
The function (sync or async) to wrap. It receives
the module's input as a |
required |
schema
|
dict
|
Optional. The target JSON schema. If not provided, use
the |
None
|
data_model
|
DataModel
|
Optional. The |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
trainable
|
bool
|
Whether the module's variables should be trainable.
Defaults to |
False
|
Source code in synalinks/src/modules/core/lambda_module.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |